<?php 
global $_GPC, $_W;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
$shopid=check_right(0);
if($operation=="dispaly"){


}elseif ($operation == 'salepage') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 10;
    switch(intval($_GPC['datetype'])){
        case "1":
            $statrtime=strtotime(date('Y-m-d')." 00:00:00");
            $endtime=strtotime(date('Y-m-d')." 23:59:59");
            break;
        case "2":
            $statrtime=strtotime(date('Y-m-d',strtotime("-1 day"))." 00:00:00");
            $endtime=strtotime(date('Y-m-d',strtotime("-1 day"))." 23:59:59");
            break;
        case "3":
            $statrtime=strtotime(date('Y-m-d',strtotime("-6 day"))." 00:00:00");
            $endtime=strtotime(date('Y-m-d')." 23:59:59");
            break;
        case "4":
            $statrtime=strtotime(date('Y-m')."-01 00:00:00");
            $temp1=date('Y-m',strtotime("+1 month"))."-1";
            $temp=date("Y-m-d",strtotime($temp1."-1 day"));
            $endtime=strtotime($temp." 23:59:59");
            break;
        case "5":
            $statrtime=strtotime(date('Y-m-d',$_GPC['statrtime'])." 00:00:00");
            $endtime=strtotime(date('Y-m-d',$_GPC['endtime'])." 23:59:59");
            break;
    }
    $condition = " and createtime>='".$statrtime."' and createtime<='".$endtime."' ";
    //if($_GPC['keyword'])$condition .= " and (out_trade_no='".$_GPC['keyword']."') ";
    //if(isset($_GPC['paytype']))$condition .= " and paytype = '" . $_GPC['paytype'] . "' ";
    $list = pdo_fetchall("SELECT * FROM " . tablename('j_hmoney_takeoutorder') . " WHERE platform=1 $condition order by id desc  LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('j_hmoney_takeoutorder') . " WHERE platform=1 $condition ");

    /**/
    $totalItem=pdo_fetch("SELECT sum(originalPrice) as fee1,sum(totalPrice) as fee2,sum(deliverFee) as fee3,sum(income) as fee4,count(*)as num1 FROM " . tablename('j_hmoney_takeoutorder') . " WHERE platform=1 $condition and status='settled' ");

    $totalgoods=pdo_fetch("SELECT sum(quantity) as num1,sum(total) as fee1 FROM " . tablename('j_hmoney_takeoutordergoods') . " WHERE orderid in (SELECT orderid FROM " . tablename('j_hmoney_takeoutorder') . " WHERE platform=1 $condition and status='settled') ");

    die(json_encode(array("success"=>true,"list"=>$list,"total"=>$total,"item1"=>$totalItem,"item2"=>$totalgoods,"a"=>$temp)));

} elseif ($operation == 'getorderdetail') {

    $orderid=intval($_GPC['orderid']);
    $list = pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_takeoutordergoods') . " WHERE orderid=:a order by id asc",array(":a"=>$orderid));
    die(json_encode(array("success"=>true,"list"=>$list)));

}elseif ($operation == 'downdata') {
    require "../addons/j_hmoney/plug/plug_elm/Config.php";
    require "../addons/j_hmoney/plug/plug_elm/OAuth_Client.php";
    require "../addons/j_hmoney/plug/plug_elm/util.php";
    require "../addons/j_hmoney/plug/plug_elm/RpcClient.php";
    require "../addons/j_hmoney/plug/plug_elm/RpcService.php";
    require "../addons/j_hmoney/plug/plug_elm/OrderService.php";
    $shopid=$_GPC["shopid"];
    $isseting=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_systemsetting')." order by id asc limit 1");
    $app_key = $isseting["elem_key"];
    $app_secret = $isseting["elem_secret"];
    $sandbox = $isseting['elemdebug'] ? true : false;
    $scope = "all";
    $config = new Config($app_key, $app_secret, $sandbox);
    $shopItem=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_elemtoken')." where groupid=:a",array(":a"=>$shopid));
    $tokenItem = get_token_by_shopid($shopItem["shopid"]);
    if(TIMESTAMP>$tokenItem->expires_in){
        $client=new OAuth_Client($config);
        $returnstr = $client->get_token_by_refresh_token($tokenItem->refresh_token, "all");
        $token=$returnstr->access_token;
        $data=array(
            "access_token"=>$returnstr->access_token,
            "expires_in"=>($returnstr->expires_in)+TIMESTAMP,
            "refresh_token"=>$returnstr->refresh_token,
        );
        pdo_update("j_hmoney_elemtoken",$data,array("id"=>$shopItem["id"]));
    }else{
        $token=$tokenItem;
    }
    $startdate=$_GPC['start'];
    $len=$_GPC["num"];
    $attechDate=date('Y-m-d',strtotime('+'.$len.' day',strtotime($startdate)));
    $pagesize=50;
    $pageindex=intval($_GPC['pageindex']);
    $order_service = new OrderService($token,$config);
    $list=$order_service->get_all_orders($shopItem["shopid"],$pageindex,$pagesize,$attechDate);
    if($list->total>0){
        $total=$list->total;
        $needpage= $total>$pagesize ? true : false;
        $listall=$list->list;
        foreach($listall as $order){
            pdo_delete("j_hmoney_takeoutorder",array("orderid"=>$order->id));
            pdo_delete("j_hmoney_takeoutordergoods",array("orderid"=>$order->id));
            $data=array(
                "platform"=>1,
                "weid"=>$_W["uniacid"],
                "groupid"=>$shopid,
                "userId"=>$order->userId,
                "shopid"=>$order->shopId,
                "orderid"=>$order->id,
                "createtime"=>strtotime($order->createdAt),
                "activetime"=>strtotime($order->activeAt),
                "deliverFee"=>format_price($order->deliverFee),
                "description"=>$order->description,
                "mobile"=>implode(",",$order->phoneList),
                "status"=>$order->status,
                "refundStatus"=>$order->refundStatus,
                "totalPrice"=>format_price($order->totalPrice),
                "originalPrice"=>format_price($order->originalPrice),
                "realname"=>$order->consignee,
                "deliveryGeo"=>$order->deliveryGeo,
                "deliveryPoiAddress"=>$order->deliveryPoiAddress,
                "income"=>format_price($order->income),
                "serviceRate"=>format_price($order->serviceRate),
                "serviceFee"=>format_price($order->serviceFee),
                "hongbao"=>format_price($order->hongbao),
                "packageFee"=>format_price($order->packageFee),
                "activityTotal"=>format_price($order->activityTotal),
                "shopPart"=>format_price($order->shopPart),
                "elemePart"=>format_price($order->elemePart),
                "VipDeliveryFeeDiscount"=>format_price($order->vipDeliveryFeeDiscount),
                "cancelOrderDescription"=>$order->cancelOrderDescription,
                "cancelOrderCreatedAt"=>$order->cancelOrderCreatedAt,
            );
            pdo_insert("j_hmoney_takeoutorder",$data);
            $goodsbox=$order->groups;
            foreach($goodsbox as $goodslist){
                foreach($goodslist->items as $goods){
                    $gooddata=array(
                        "orderid"=>$order->id,
                        "shopid"=>$order->shopId,
                        "goodid"=>$goods->id,
                        "skuid"=>$goods->skuId,
                        "title"=>$goods->name,
                        "pcate"=>$goods->categoryId,
                        "price"=>format_price($goods->price),
                        "quantity"=>$goods->quantity,
                        "total"=>format_price($goods->total),
                        "userprice"=>format_price($goods->userPrice),
                        "shopprice"=>$goods->shopPrice,
                        "additions"=>implode(",",$goods->additions),
                        "newspecs"=>implode(",",$goods->newSpecs),
                        "attributes"=>implode(",",$goods->attributes),
                    );
                    pdo_insert("j_hmoney_takeoutordergoods",$gooddata);
                }
            }
        }
        die(json_encode(array("success"=>true,"needpage"=>$needpage)));
    }
    die(json_encode(array("success"=>true)));
}
include $this->template('web/elem');









